import React from 'react'
import {
  TouchableOpacity,
  View,
  Text,
  ScrollView,
} from 'react-native';

import CommonDataEditListPage from '../common/CommonDataEditListPage';
import Config from '../../config';
import {ObjUtils, DateUtils} from '../../utils/Utils';
import SizeUtils from '../../common/SizeUtils';
import ToastUtils from '../../common/ToastUtils';
import HeartConstants from '../../common/HeartConstants';
import CommonShowPickerAction from '../../actions/common/CommonShowPickerAction';

const DEFAULT_TXT = "不限";

//爱情事搜索条件
export default class HeartLoveSearchConditionPage extends CommonDataEditListPage {
  constructor(props) {
    super(props);

    this.state.isUpdate = false;
    this._isShowSubmit = true;
    this._submitText = "确 定";
    this._showNeedMark = false;
  }

  _initPageButtonValues() {
    this._pageButtonValues = {};
    let conditionDatas = this._getPageDatas();
    for (var key in conditionDatas) {
      this._pageButtonValues[key] = key==='workerNum'?'':DEFAULT_TXT;
    }
  }

  _getPageDatas() {
    return HeartConstants.searchConditionData1;
  }

  componentWillUpdate(nextProps, nextState) {
    super.componentWillUpdate(nextProps, nextState);
    nextState.isUpdate = false;
  }

  // _getPageButtonValue(data) {
  //   if(this.state[data.key] == null) {
  //     return DEFAULT_TXT;
  //   }
  //   if(data.type == "datePicker") {
  //     if(this.state[data.key] != null) {
  //       return DateUtils.formatDate(this.state[data.key], "yyyy年MM月dd日");
  //     }
  //   }
  //   return this.state[data.key];
  // }
  _renderPageBottom(){
      return(<View style={{height:50}}/>)
  }
  _showPick(params, data) {
    if(params.pickerData instanceof Array) {
      params.pickerData.splice(0, 0, DEFAULT_TXT);
    }
    super._showPick(params, data);
  }

  _onPressSubmit() {
    let sendData = this._dealSendData();
    if(sendData == null) {
        ToastUtils.show("必须填一个条件");
    }
    else {
      this._gotoEdit = true;
      this.gotoPage(Config.pages.heart_love_search_result, {condition:sendData});
    }
  }

  _dealSendData() {
    let constantsData = null;
    let sendData = {};
    let paramsCount = 0;
    for (var type in HeartConstants.searchConditionData1) {
      constantsData = HeartConstants.searchConditionData1[type];
      if(this._pageButtonValues.hasOwnProperty(type) && this._pageButtonValues[type] != DEFAULT_TXT&& this._pageButtonValues[type] != '') {
        paramsCount += 1;
        if(constantsData.type == "picker") {
          let index = constantsData.params.indexOf(this._pageButtonValues[type]);
          sendData[type] = index + 1;
        }
        else {
          sendData[type] = this._pageButtonValues[type];
        }

        if(constantsData.sendType == "int") {
          sendData[type] = parseInt(sendData[type]);
        }
        else if(constantsData.sendType == "array") {
          let array = [sendData[type]];
          sendData[type] = array;
        }
      }
    }
    if(paramsCount > 0) {
      return sendData;
    }
    else {
      return null;
    }
  }
}
